package com.mellarius.Adaptor;

import java.util.ArrayList;
import java.util.List;

import com.helper.Hive;
import com.mellarius.R;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class SelectHiveAdaptor extends ArrayAdapter<Hive> {
	List<Hive> hives = new ArrayList<Hive>();
	List <PlanetViewHolder> cbList = new ArrayList<PlanetViewHolder>();
	Context context;
	View convertView;
	int size;
	
	public SelectHiveAdaptor(Context context, List<Hive> resource) {
		super(context, R.layout.listview_checkbox, resource);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.hives = resource;

	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Hive hive = (Hive) this.getItem(position);
		TextView name;
		CheckBox cb;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.listview_checkbox, parent, false);
			name = (TextView) convertView.findViewById(R.id.textViewSelectHive);
			cb = (CheckBox) convertView.findViewById(R.id.checkBoxSelectHive);
			convertView.setTag(new PlanetViewHolder(name, cb));
			cb.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					CheckBox cb = (CheckBox) v;
					Hive hive = (Hive) cb.getTag();
					if (cb.isChecked()) {
						hive.setChecked(cb.isChecked());
					} else {
						hive.setChecked(false);
					}
				}
			});
		} else {
			// Because we use a ViewHolder, we avoid having to call
			// findViewById().
			PlanetViewHolder viewHolder = (PlanetViewHolder) convertView.getTag();	
			cb = viewHolder.getCheckBox();
			name = viewHolder.getTextView();
			
		}
		// Tag the CheckBox with the Hive it is displaying, so that we can
		// access the hive in onClick() when the CheckBox is toggled.
		cb.setTag(hive);
		// Display hive data
		name.setText(hive.getName());
		cb.setChecked(hive.isChecked());

		
		return convertView;
	}


	/** Holds child views for one row. */
	private static class PlanetViewHolder {
		private CheckBox checkBox;
		private TextView textView;

		public PlanetViewHolder(TextView textView, CheckBox checkBox) {
			this.checkBox = checkBox;
			this.textView = textView;
		}

		public CheckBox getCheckBox() {
			return checkBox;
		}

		public TextView getTextView() {
			return textView;
		}

	}
}
